// noinspection DuplicatedCode

import { defineStore } from "pinia";
import { UserRequestLogVO } from "@/entiy/vo/UserRequestLogVO.ts";
import XEUtils from "xe-utils";
import { VxeFormPropTypes } from "vxe-pc-ui";
import RequestService from "@/service/RequestService.ts";
import { PageVO } from "@/entiy/vo/PageVO.ts";
import PromiseUtils from "@/utils/PromiseUtils.ts";

export const useRequestLogStore = defineStore("useRequestLogStore", {
    state: () => {
        return {
            isPageLoading: false,
            isTableLoading: false,

            // 表单数据
            startDateTime: "",
            endDateTime: "",

            // 分页数据
            currentPage: 1,
            currentPageSize: 10,
            totalRow: 0,

            // 表格数据
            tableDataList: [] as Array<UserRequestLogVO>,

            // 表单校验规则
            formRules: {
                startDateTime: [
                    {required: true, content: '开始日期不能为空'},
                    {
                        validator({itemValue}) {
                            if (XEUtils.isNull(itemValue) || !XEUtils.isValidDate(new Date(itemValue))) {
                                return new Error('请输入一个合法的日期')
                            }
                        }
                    }
                ],

                endDateTime: [
                    {required: true, content: '结束日期不能为空'},
                    {
                        validator({itemValue}) {
                            if (XEUtils.isNull(itemValue) || !XEUtils.isValidDate(new Date(itemValue))) {
                                return new Error('请输入一个合法的日期')
                            }
                        }
                    }
                ]
            } as VxeFormPropTypes.Rules
        }
    },

    getters: {},

    actions: {
        async queryUserRequestLogList(): Promise<void> {
            const result = await RequestService.post<PageVO<UserRequestLogVO>>("/get-user-request-log-list", {});
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.tableDataList = result.bizDta.recordList;
            this.totalRow = result.bizDta.totalRow;
            return PromiseUtils.resolve();
        }
    }
});
